/**
 * 指定日期增加n月
 * @param dateStr 指定日期
 * @param num 月
 * @param type 0:2023-01-30   1：2022-01-30 11:11:11
 * @returns {string}
 * @author: Leo
 */

export function commonAddMouth(dateStr, num, type) {
    var monthNum = 0;
    if (typeof num == "string") {
      monthNum = parseInt(num);
    } else {
      monthNum = num;
    }
    var date = new Date(dateStr);
    //获取原日
    var day = date.getDate();
    //获取原月份
    var month = date.getMonth();
    //设置增加月份
    date.setMonth(date.getMonth() + monthNum * 1, 1);
    //获取增加的后的月份
    var Jmonth = date.getMonth() + 1;
    //获取增加的后的年份
    var Jyear = date.getFullYear();
    if (Jmonth == 4 || Jmonth == 6 || Jmonth == 9 || Jmonth == 11) {
      //小月
      if (day > 30) {
        day = 30;
      }
    } else if (Jmonth == 2) {
      //2月判断是否闰年
      if ((Jyear % 4 == 0 && Jyear % 100 != 0) || Jyear % 400 == 0) {
        if (day > 29) {
          day = 29;
        }
      }
      if (day > 28) {
        day = 28;
      }
    } else {
      //大月
      if (day > 31) {
        day = 31;
      }
    }
    var tHours = date.getHours();
    var tMinutes = date.getMinutes();
    var tSeconds = date.getSeconds();
    tHours = tHours < 10 ? "0" + tHours : tHours;
    tMinutes = tMinutes < 10 ? "0" + tMinutes : tMinutes;
    tSeconds = tSeconds < 10 ? "0" + tSeconds : tSeconds;
    Jmonth = doHandleMonth(Jmonth);
    day = doHandleMonth(day);
    if (type == "0") {
      return `${Jyear}-${Jmonth}-${day}`;
    }
  
    return `${Jyear}-${Jmonth}-${day} ${tHours}:${tMinutes}:${tSeconds}`;
  }
  
  /**
   *  日或月补0
   * @param month
   * @returns {string}
   */
  function doHandleMonth(month) {
    var m = month;
    if (month.toString().length == 1) {
      m = "0" + month;
    }
    return m;
  }
  